package cate.game.play.skill.passive.hero;

import cate.common.table.d.GDPos;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.fighter.Fighter;
import cate.game.play.proce.action.ctx.ActionCtx;
import cate.game.play.skill.passive.PassiveHandler;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 每回合选择我方生命值最低的2个英雄，若其生命值低于60%，则附加【绝对援护】并提升受援护者20%免伤，优先保护后排
 */
public class 无畏战甲PH extends PassiveHandler {


	private int 人数;

	private int 血量;

	private int buff;

	//人数=2&血量=6000&buff=？？
	@Override
	public void parse(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		人数 = args.getInt("人数", 0);
		血量 = args.getInt("血量", 0);
		buff = args.getInt("buff", 0);
	}

	@Override
	public void parseExt(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		血量 += args.getInt("血量", 0);
	}

	@Override
	public void onRoundBegin(ActionCtx action) {
		if (skill.owner.isDead()) {
			return;
		}
		int tarNum = 人数;
		List<Fighter> friends = skill.owner.getFightTeam().getFriendsAlive(skill.owner, true);
		List<Fighter> targets = friends.stream().filter(f -> f.getPos() >= GDPos.P4).
				sorted(Comparator.comparingDouble(f -> f.attr.hpCur())).limit(tarNum).collect(Collectors.toList());

		tarNum -= targets.size();
		if (tarNum > 0) {
			targets.addAll(friends.stream().filter(f -> f.getPos() <= GDPos.P3).
					sorted(Comparator.comparingDouble(f -> f.attr.hpCur())).limit(tarNum).collect(Collectors.toList()));
		}

		for (Fighter target : targets) {
			if (target.attr.total().hpCurRate() <= 血量) {
				target.buff.tryAddByTid(action, skill.owner, buff);
			}
		}
	}
}
